//
//  Logger.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/4.
//

import Foundation
import os.log

/// 日志级别
//enum LogLevel: String {
//    case debug = "🐛 DEBUG"
//    case info = "ℹ️ INFO"
//    case warning = "⚠️ WARNING"
//    case error = "❌ ERROR"
//}

enum LogTag: String {
    case wechat = "casdon_weChat"
    case netWork = "casdon_netWork"
    case appLife = "casdon_appLife"
    case encrypt = "casdon_encrypt"
    case viewAbout = "casdon_viewAbout"
    case oneKeyView = "casdon_oneKeyView" // 一键登录
    case login = "casdon_login"
    case uMeng = "casdon_uMeng"
    case home = "casdon_home"
}

enum LogLevel: Int {
    case info = 1
    case debug
    case warning
    case error
    
    var prefix: String {
        switch self {
        case .info:
            return "INFO"
        case .debug:
            return "DEBUG"
        case .warning:
            return "WARNING"
        case .error:
            return "ERROR"
        }
    }
    
    var icon: String {
        switch self {
        case .info:
            return "ℹ️"
        case .debug:
            return "🔍"
        case .warning:
            return "⚠️"
        case .error:
            return "❌"
        }
    }
}

final class Logger {
    static let shared = Logger()
    private let log: OSLog
    private let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm:ss.SSS"
        return formatter
    }()
    
    private init() {
        let bundleID = Bundle.main.bundleIdentifier ?? "com.casdon.app"
        self.log = OSLog(subsystem: bundleID, category: "AppLog")
    }
    
    // 用来控制日志级别
    static var currentLogLevel: LogLevel = .info
    
    // 日志打印方法
    static func log(_ level: LogLevel, message: String? = nil, file: String = #file, line: Int = #line, function: String = #function, tag: LogTag) {
        // 如果当前日志级别低于设置的级别，则不打印
        guard level.rawValue >= currentLogLevel.rawValue else { return }
        
        // 获取文件名
        let fileName = (file as NSString).lastPathComponent
        
        // 格式化时间
        let timestamp = shared.dateFormatter.string(from: Date())
        
        // 构建日志消息
        let logMessage = "\(level.icon) [\(timestamp)] [\(level.prefix)] [\(tag.rawValue)] [\(fileName):\(line)] \(function) - \(message ?? "")"
        
        // 使用 OSLog 记录日志
        os_log("%{public}@", log: shared.log, type: level == .error ? .error : .default, logMessage)
        
        // 在 DEBUG 模式下输出日志
        #if DEBUG
        //print(logMessage)
        #endif
    }
    
    // 便捷方法
    static func debug(_ message: String, file: String = #file, line: Int = #line, function: String = #function, tag: LogTag) {
        log(.debug, message: message, file: file, line: line, function: function, tag: tag)
    }
    
    static func info(_ message: String, file: String = #file, line: Int = #line, function: String = #function, tag: LogTag) {
        log(.info, message: message, file: file, line: line, function: function, tag: tag)
    }
    
    static func warning(_ message: String, file: String = #file, line: Int = #line, function: String = #function, tag: LogTag) {
        log(.warning, message: message, file: file, line: line, function: function, tag: tag)
    }
    
    static func error(_ message: String, file: String = #file, line: Int = #line, function: String = #function, tag: LogTag) {
        log(.error, message: message, file: file, line: line, function: function, tag: tag)
    }
}

